package com.chunguang.netty.comon;

import java.util.concurrent.TimeUnit;

import com.chunguang.netty.domain.RequestData;

import io.netty.channel.ChannelFuture;

public class MainThread implements Runnable {
	QueueSiglon queue = null;
	 
	 Client client = null;
	public MainThread() {
		queue = QueueSiglon.getInstance();
		  client = Client.getInstance(); 
	}


	@Override
	public void run() {

		ChannelFuture channelFuture= null;
		while (true) {
			int sleep =0;
			while (queue.size() > 0) {
				 if (channelFuture==null) {
					 channelFuture = client.getChannelFuture();
				}
				 RequestData requestData = queue.get();
				 channelFuture.channel().writeAndFlush(requestData);
				 System.out.println("send+" + requestData.getCheckNumber());
				--sleep;
			}
			try {
				TimeUnit.SECONDS.sleep(1);
				sleep++;
				System.err.println("sleep ......");
				if (sleep>=5) {
					channelFuture.channel().closeFuture().sync();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}

	
}
